package com.zp.servlet;

import com.google.gson.Gson;
import com.zp.utils.RegexUtil;
import com.zp.utils.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user/check")
public class CheckServlet extends HttpServlet {

    private PrintWriter out;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        out = response.getWriter();
        Gson gson = new Gson();

        // 获取传送过来的参数
        String sno = request.getParameter("sno");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String mail = request.getParameter("mail");

        Boolean isSnoMatch;
        Boolean isPswMatch;
        Boolean isPhoneMatch;
        Boolean isMailMatch;

        Map<String, Boolean> data = new HashMap<>();

        if(sno != null) {
            isSnoMatch = RegexUtil.checkSnoRegex(sno);
            data.put("sno", isSnoMatch);
        }
        if(password != null) {
            isPswMatch = RegexUtil.checkPswRegex(password);
            data.put("password", isPswMatch);
        }
        if(phone != null) {
            isPhoneMatch = RegexUtil.checkPhoneRegex(phone);
            data.put("phone", isPhoneMatch);
        }
        if(mail != null) {
            isMailMatch = RegexUtil.checkMailRegex(mail);
            data.put("mail", isMailMatch);
        }
        out.print(gson.toJson(ResultUtil.success(data)));
    }
}
